package com.csi.sai.bl.login;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.csi.sai.utilities.Constants;
import com.csi.sai.utilities.Utilities;

public class LoginProcess {
	
	private String user;
	private String pass;
	private int type;
	private String userName;
	private int idUser;
	
	public int getType(){ return this.type; }
	public String getUserName(){ return this.userName; }
	public int getIdUser(){ return this.idUser; }

	public LoginProcess(String user, String pass) {
		this.user = user;
		this.pass = pass;
	}

	public boolean userExists(DataSource ds) {
		Connection conn = null;
		ResultSet rs = null;
		PreparedStatement ps = null;
		
		try {
			conn = ds.getConnection();
			ps = conn.prepareStatement(Constants.CALL_PROCEDURE_LOGIN);
			ps.setString(1, this.user);
			ps.setString(2, this.pass);
			rs = ps.executeQuery();
			
			if(rs.next()) {
				type = rs.getInt(1);
				userName = rs.getString(2)+" "+rs.getString(3);
				idUser = rs.getInt(4);
				return true;
			} 
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			Utilities.finallyProcess(conn, ps, rs);
			Utilities.finallyConnection(conn);
		}
		
		return false;
	}

}
